package teleradiologia.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;

import teleradiologia.repository.PersonDao;

public class MyUserDetailsService implements UserDetailsService {
	private PersonDao personDao;

	private static final Log log = LogFactory
			.getLog(MyUserDetailsService.class);

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {

		UserDetails result = this.personDao.getByLogin(username);

		if (result == null) {
			log.error("No such user");
			throw new UsernameNotFoundException("No such user");
		}

		return result;
	}
}
